<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/22
 * Time: 11:24
 */

class Siteweb_model extends CI_Model
{

    public $table_name = 'siteweb';

    public function siteweb_info($params = array(), $is_all = 0)
    {
        $this->db->from('siteweb');
        $this->db->where($params);

        $query = $this->db->get();
    
        $siteweb = $query->row_array();
        
        if ($is_all == 1 && $siteweb) {
            $siteweb['info'] = $this->info($siteweb['web_id']);
        }

        return $siteweb;
    }
    
    public function info($web_id)
    {
        $query = $this->db->from('siteweb_info')->where('web_id', $web_id)->get();
        
        $info = $query->row_array();
        if ($info) {
            $info['cate_arr'] = $info['cate'] ? unserialize($info['cate']) : array();
            $info['tags_arr'] = explode(',', $info['tags']);
        }
        
        return $info;
    }

    public function insert_siteweb($data, $web_id)
    {
        $time = time();

        $_data = array(
            'web_name' => $data['web_name'],
            'title' => $data['title'],
            'ab_name' => $data['ab_name'],
            'web_access' => $data['web_access'],
            'cate' => $data['cate'],
            'sort_num' => $data['sort_num'],
            'type' => $data['type'],
            'view_id' => $data['view_id']
        );

        if ($web_id) {

            $_data = array_filter($_data);

            $this->db->where('web_id', $web_id);
            $this->db->update('siteweb', $_data);
        } else {

            $_data['add_time'] = $time;

            $this->db->insert('siteweb', $_data);
            $web_id = $this->db->insert_id();
        }

        return $web_id;
    }
    
    public function siteweb_list($params = array())
    {
        $query = $this->db->from('siteweb')->where($params)->get();
        $_list = $query->result_array();
        $list = array();
        foreach ($_list as $row) {
            $list[$row['web_id']] = $row;
        }
        
        return $list;
    }

    public function siteweb_option($is_all = 0, $params = array())
    {
        if($is_all) {
            $params['is_delete'] = 0;
        }


        $data = $this->j_list($params, '*', 0, 1000, 'sort_num');

        $siteweb = array();
        foreach ($data as $row) {
            $siteweb[$row['web_id']] = $row['web_name'];
        }

        return $siteweb;
    }
}
